/*
 *  LwIP for a modified Cv32e40p (RV32IMC) Processor Core.
 *
 *  Copyright (C) 2021-2022 Junnan Li <lijunnan@nudt.edu.cn>.
 *  Copyright and related rights are licensed under the MIT license.
 *
 *  Date: 2022.10.13
 *  Description: port LwIP to RISC-V MCU. 
 */

#include "firmware.h"
#include "pkt_proxy.h"

void show_pkt(unsigned char *pkt, int pkt_len);

//unsigned int test_pkt[128] = {0x12345678, 0x12345678, 0x12345678, 0x12345678};

int main(){
    //* system inilization, open all interrupt (32_bitmap);
    irq_init();
    
    //* for timer_irq;
    timer_irq_count = 0;
    
    printf("\rsystem boot finished\r\n");
    
    //* set timer_irq_value, i.e., SYSTICK_ADDR
    *((volatile uint32_t *) SYSTICK_ADDR) = 100000000;
    
    //* enable dma;
    *((volatile uint32_t *) DMA_START_EN) = 0x1234;
    *((volatile uint32_t *) DMA_START_EN) = 1;
    
    //* recv buffer;
    unsigned char buf_recv_pkt[1600];
    //unsigned char buf_send_pkt[1600];
    //int i;
    
    unsigned int err = 0;
    unsigned int get_pkt_len = 0;

    int recv_udp = 0;
    int recv_tsmp = 0;
    int send_udp = 0;
    int send_tsmp = 0;

    while(1){
        get_pkt_len = (unsigned int)(rv_recv((unsigned int *)buf_recv_pkt));
        
        if(get_pkt_len > 0)
        {
            //* monitor pkt
		if((buf_recv_pkt[12] != 0x90) && (buf_recv_pkt[13] != 0x06)){
		   	rv_send_monitor((unsigned int *)buf_recv_pkt, get_pkt_len);
		}

            if((buf_recv_pkt[12] == 0x08) && (buf_recv_pkt[13] == 0x06))
            {
                err = arp_reply(buf_recv_pkt, arp_reply_pkt);
                
                if(err == 0) continue;
                else rv_send((unsigned int *)arp_reply_pkt, err);
            }
            else if((buf_recv_pkt[12] == 0x08) && (buf_recv_pkt[13] == 0x00) && (buf_recv_pkt[23] == 0x11))
            {
                recv_udp++;
                //show_pkt((unsigned char *)buf_recv_pkt, get_pkt_len);
                
                err = udp_relay_tsmp(buf_recv_pkt, udp_send_pkt, get_pkt_len);
                
                if(err == 0) continue;
                else {
			rv_send((unsigned int *)(buf_recv_pkt + 42), err);
			//* monitor pkt
		   	rv_send_monitor((unsigned int *)(buf_recv_pkt + 42), err);
		}

                //show_pkt((unsigned char *)(buf_recv_pkt + 42), err);
                
                send_tsmp++;
                
                //printf("recv_udp: %d, send_tsmp: %d\r\n", recv_udp, send_tsmp);
            }
            else if((buf_recv_pkt[12] == 0xFF) && (buf_recv_pkt[13] == 0x01))
            {
                recv_tsmp++;
                //show_pkt((unsigned char *)buf_recv_pkt, get_pkt_len);
                
                err = tsmp_relay_udp(buf_recv_pkt, udp_send_pkt, get_pkt_len);
                
                if(err == 0)
		{
			printf("Recv[12:13]=0xFF01: err!\n\r");
			continue;
		}
                else
		{
			rv_send((unsigned int *)udp_send_pkt, err);
			//* monitor pkt
		   	rv_send_monitor((unsigned int *)udp_send_pkt, err);
			//show_pkt((unsigned char *)udp_send_pkt, err);
		}
                                
                send_udp++;
                
                //printf("recv_tsmp: %d, send_udp: %d\n\r", recv_tsmp, send_udp);
            }
            else
            {
                printf("zabao: %d\n\r",get_pkt_len);
            }
            
            get_pkt_len = 0;
        }
    }
    
    return 0;
}

void show_pkt(unsigned char *pkt, int pkt_len)
{
    int i=0;
    printf("-----------------------*******-----------------------\n\r");
    //printf("Packet Addr:%8x\n",(unsigned long)pkt);
    for(i=0;i<16;i++)
    {
        if(i % 16 == 0)
            printf("      ");
        printf(" %x ",i);
        if(i % 16 == 15)
            printf("\n\r");
    }

    for(i=0;i<pkt_len;i++)
    {
        if(i % 16 == 0)
            printf("%4x: ",i);
        printf("%2x ",*(pkt+i));
        if(i % 16 == 15)
            printf("\n\r");
    }
    if(pkt_len % 16 !=0)
        printf("\n\r");
    printf("-----------------------*******-----------------------\n\r\n\r");
}
